﻿/*
 * Copyright 2011 Hanns Kucer
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.Text;
using iTachToolkit;

namespace iTachSpoofer
{
    class Program
    {
        // our own implementation of a virtual iTach
        class MyiTach : iTach.IpiTach
        {
			int i = 0;

			public MyiTach()
				: base(new EndPoint.IpEndPoint("192.168.0.5"))
			{
			}

            // called from a thread internal to the Toolkit
            public override string Trancieve(string data)
            {
                // you could simply do this
                //return base.Trancieve(data)

                // but it's more fun to inspect the data passing through the iTach
                var trx = new Transmission(data, base.Trancieve(data));

				Console.WriteLine(i.ToString() + ": " + trx.Request.Data);
				i++;

                // finally return the data
                return trx.Response.Data;
            }
        }

        // our own implementation of a virtual endpoint
        class MyEndPoint : EndPoint.VirtualEndPoint
        {
            // called from a thread internal to the Toolkit
            public override iTach iTachFactory()
            {
                // return the "special" iTach
				return new MyiTach();
            }
        }

        static void Main(string[] args)
        {
            // create and start a new proxy server and run, this never returns unless you
            // stop the proxy from another thread effectively killing the main thread and
            // thus also the whole application itself.

			// this is our endpoint
			var localEp = new EndPoint.IpEndPoint("192.168.0.4");

			// this is enndpoint of the actual iTach
			var iTachEp = new MyEndPoint();

			// now, start a proxy for the two
			new Proxy(localEp, iTachEp, true).Start(true);
		}

    }
}
